/*
 * @Date: 2021-05-27 12:29:22
 * @LastEditTime: 2021-09-07 17:27:32
 */
/* eslint-disable no-unused-vars */
import { Message } from 'element-ui'
const validateDocumentType = (
  files,
  filesType,
  type = 'warning',
  tips = '上传文件格式不正确！请重新上传'
) => {
  // 预设文件类型检验
  console.log('warning----------------')
  const isppt = (fileType) => fileType === 'application/vnd.openxmlformats-officedocument.presentationml.presentation'
  const isexcel = (fileType) => fileType === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
  const istxt = (fileType) => fileType === 'text/plain'
  const ispdf = (fileType) => fileType === 'application/pdf'
  const isimg = (fileType) => fileType === 'image/jpeg' || fileType === 'image/png'
  const isword = (fileType) => fileType === 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
  const isdoc = (fileType) => fileType === 'application/msword'
  // const presetTypes = ['ppt', 'excel', 'txt', 'pdf', 'img', 'word', 'doc']
  const typeMap = {
    video: ['video/3gpp', 'video/mpeg', 'video/mp4', 'video/mpeg', 'video/mpeg'],
    audio: ['audio/ac3', 'audio/basic', 'audio/mpeg', 'audio/mpeg', 'audio/mp4', 'audio/ogg'],
    allpication: ['allpication/vnd.ms-asf'],
    text: ['text/css', 'text/csv', 'text/html', 'text/html', 'text/javascript', 'text/rtf', 'text/plain', 'text/xml'],
    application: ['application/xhtml+xml', 'application/zip', 'application/x-zip-compressed', 'application/msword', 'application/msword', 'application/xml-dtd', 'application/javascript', 'application/json', 'application/vnd.ms-project', 'application/ogg', 'application/pdf', 'application/vnd.ms-powerpoint', 'application/vnd.ms-powerpoint', 'application/vnd.ms-powerpoint', 'application/rtf', 'application/vnd.ms-works', 'application/vnd.ms-works', 'application/vnd.ms-excel', 'application/vnd.ms-excel', 'application/vnd.ms-excel', 'application/vnd.ms-excel', 'application/vnd.ms-excel', 'application/xml', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'],
    image: ['image/vnd.dwg', 'image/vnd.dxf', 'image/gif', 'image/jp2', 'image/jpeg', 'image/jpeg', 'image/jpeg', 'image/png', 'image/vnd.svf', 'image/tiff', 'image/tiff']
  }
  let presetTypes = []
  for (const key in typeMap) {
    presetTypes = presetTypes.concat(typeMap[key])
  }
  const mixin = filesType.filter((item) => presetTypes.indexOf(item) !== -1)
  console.log('mixin--------------')
  console.log(mixin)
  // 检车是否存在预设并使用哪个预设
  function presetType(fileType) {
    // 需要动态修改
    let evalStr = ''
    // eslint-disable-next-line array-callback-return
    mixin.map((mix, index) => {
      if (index < mixin.length - 1) {
        evalStr += `is${mix}('${fileType}') || `
      } else {
        evalStr += `is${mix}('${fileType}')`
      }
    })
    // eslint-disable-next-line no-eval
    const switchStatus = eval(evalStr)
    return switchStatus
  }

  // 预设文件类型校验
  function switchType(fileType) {
    // 先检验预设
    // 判断要上传的文件里有无预设检验
    // if (mixin && mixin.length > 0 && presetType(fileType)) {
    // return true
    // }
    if (filesType.indexOf(fileType) > -1) {
      return true
    }
    return false
  }

  // 循环文件
  function loopFiles(fileArray) {
    for (let i = 0; i < fileArray.length; i += 1) {
      if (!fileArray[i].type) {
        throw new Error('type is not defined')
      }
      if (switchType(fileArray[i].type)) {
        return true
      }
    }
    return false
  }

  let typeStatus

  // 上传文件数组处理
  if (Array.isArray(files)) {
    typeStatus = loopFiles(files)
  } else {
    typeStatus = loopFiles([files])
  }

  if (!typeStatus) {
    Message({
      message: tips,
      type
    })
  }

  return typeStatus
}

export default validateDocumentType
